package com.android.sq.zouqi.bdmap;
/**
 * @author rongfzh
 * @version 1.0.0  
 */
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;

/*
 * OnDoubleTapListener:onDoubleTap, onDoubleTapEvent, onSingleTapConfirmed
 * OnGestureListner:onDown, onFling, onLongPress, onScroll, onShowPress, onSingleTapUp 
 * LongPressOverlay仅仅是一个透明的Overlay依附在MapView上，它只处理长按事件的处理，或许GeoPoint的经纬度
 */
public class LongPressOverlay extends Overlay implements OnGestureListener {
	private final static int PRESS_TIME = 500; 	// 定义长按时间为500毫秒，即半秒
	private final static String TAG = "LongPressOverlay";
	private pScrChooseLocation pScrChooseLocation;
	private MapView mMapView;
	private Handler mHandler;
	private MapController mMapCtrl;
	private GestureDetector gestureScanner = new GestureDetector(this);
	private int level = 0;
	
	public LongPressOverlay(pScrChooseLocation pScrChooseLocation, MapView mapView, Handler handler,MapController mapCtrl){
		this.pScrChooseLocation = pScrChooseLocation;
		mMapView = mapView;
		mHandler = handler;
		mMapCtrl = mapCtrl;
		gestureScanner.setIsLongpressEnabled(true);
	}
	long down_time = 0;
	long up_time = 0;
	boolean isMove = false;
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		return gestureScanner.onTouchEvent(event);
		/*if (event.getPointerCount() == 1) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				down_time = event.getEventTime();
				isMove = false;
				//Log.e(TAG, "ACTION_DOWN, time-" + down_time + ", isMove-" + isMove);
				return false;
			case MotionEvent.ACTION_MOVE:
				//Log.e(TAG, "ACTION_MOVE");
				isMove = true;
				return super.onTouchEvent(event, mapView);
			case MotionEvent.ACTION_UP:
				up_time = event.getEventTime();
				//Log.e(TAG, "ACTION_UP, time-" + up_time + ", isMove = " + isMove);
				if ((!isMove) && up_time - down_time > PRESS_TIME) {
					// 处理长按事件
					//Log.e(TAG, "2000 LongPressOverlay->onLongPress");
					// 直接设置了pScrChooseLocation的lngpress_point的值 
					pScrChooseLocation.pin_point = mMapView.getProjection()
							.fromPixels((int) event.getX(), (int) event.getY());
					Log.e(TAG, "长按得到的point=" + pScrChooseLocation.pin_point.getLatitudeE6() + ", " 
							+ pScrChooseLocation.pin_point.getLongitudeE6());
					pScrChooseLocation.isCurLocate = false;
					mHandler.sendEmptyMessage(pScrChooseLocation.MSG_VIEW_LONGPRESS);
				}
				return false;
			}
		}
		return super.onTouchEvent(event, mapView);*/
	}

	/*
	 * (non-Javadoc)
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
	 */
	/*@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}*/
	
	/*
	 * OnDoubleTapListener
	 */
	/*@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onDoubleTap(MotionEvent e) {
		Log.e(TAG, "LongPressOverlay->onDoubleTap双击");
		return false;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		Log.e(TAG, "LongPressOverlay->onDoubleTapEvnet");
		if(++level % 3 == 0){
			mMapCtrl.zoomIn();
			level = 0;
		}
		return false;
	}*/

	/*
	 * OnGestureListener
	 */
	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		/*Log.e(TAG, "触摸点有" + e.getPointerCount() + "个");
		if (e.getPointerCount() > 1) {
			Log.e(TAG, "触摸点大于1个了");
			return;
		}
		Log.e(TAG, "LongPressOverlay->onLongPress");
		pScrChooseLocation.lngpress_point = mMapView.getProjection().fromPixels((int) e.getX(),
				(int) e.getY());
		mHandler.sendEmptyMessage(pScrChooseLocation.MSG_VIEW_LONGPRESS);*/
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent event) {
		//Log.e(TAG, "2000 LongPressOverlay->onLongPress");
		// 直接设置了pScrChooseLocation的lngpress_point的值 
		if (event.getPointerCount() == 1) {
			pScrChooseLocation.pin_point = mMapView.getProjection()
				.fromPixels((int) event.getX(), (int) event.getY());
			Log.e(TAG, "长按得到的point=" + pScrChooseLocation.pin_point.getLatitudeE6() + ", " 
				+ pScrChooseLocation.pin_point.getLongitudeE6());
			pScrChooseLocation.isCurLocate = false;
			mHandler.sendEmptyMessage(pScrChooseLocation.MSG_VIEW_LONGPRESS);
		}
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}

}
